<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;

use Illuminate\Http\Request;
use Validator;
use Auth;

class LoginController extends Controller
{
    /*
    |--------------------------------------------------------------------------
    | Login Controller
    |--------------------------------------------------------------------------
    |
    | This controller handles authenticating users for the application and
    | redirecting them to your home screen. The controller uses a trait
    | to conveniently provide its functionality to your applications.
    |
    */

    use AuthenticatesUsers;

    /**
     * Where to redirect users after login.
     *
     * @var string
     */
    protected $redirectTo = '/';

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('guest')->except('logout');
    }

    public function showLoginForm()
    {
        return view('touch.auth');
    }

    /**
     * 手机号登陆验证
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    private function phoneValidator(array $data){
        $attribute =  array(
            'username' => '手机号',
            'password' =>  '密码',
        );

        $message  = array(
            'required' => ':attribute必须填写',
            'max' => '最大:max字符',
            'min' => '最小:min字符',
            'regex' => '手机号格式错误',
        );

        return Validator::make($data, [
            'username' => ['regex:/^(139|138|137|136|135|134|147|150|151|152|157|158|159|178|182|183|184|187|188|130|131|132|155|156|185|186|145|176|133|153|177|173|180|181|189|170|171)\d{8}$/'],
            'password' => 'bail|required|min:6|max:20',
        ],$message,$attribute);
    }

    /**
     * 手机号验证并登陆
     * @param  [type] $request [description]
     * @return [type]          [description]
     */
    private function phoneLogin($request){
        $username = $request->input('username');
        $pwd = $request->input('password');
        $validator = $this->phoneValidator($request->all());
        if($validator->fails()){
            $this->ajaxJson(10,$validator->errors()->first());
        }
        $validator->errors()->add('login_fail','登录失败,用户名或密码错误!');
        if(!Auth::attempt(['phone' => $username, 'password' => $pwd], $request->has('remember'))){
            $this->ajaxJson(10,$validator->errors()->first());
        }
        return $this->ajaxJson(0,'登陆成功',[],false);
    }

    public function logout(Request $request)
    {
        $this->guard()->logout();
        $request->session()->flush();
        $request->session()->regenerate();
        return $this->ajaxJson(0,'退出成功',[],false);
    }

    /**
     * 重写登陆
     * @param  Request $request [description]
     * @return [type]           [description]
     */
    public function login(Request $request)
    {
        return $this->phoneLogin($request);     
    }

}
